package com.liao.search.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.liao.common.doc.ProductDoc;
import com.liao.common.dto.ProductSearchByCategoryRequest;
import com.liao.common.dto.ProductSearchRequest;
import com.liao.common.vo.SearchResult;
import com.liao.search.service.HistoryKeywordSaveSaveService;
import com.liao.search.service.ProductService;
import com.liao.search.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @Author: ljt
 * @Description: 用户搜索相关接口
 * @DateTime: 2025/3/12 下午9:15
 **/
@Slf4j
@RequestMapping("/search/member")
@RestController
public class searchController {


    @Autowired
    private ElasticsearchClient client;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private HistoryKeywordSaveSaveService historyKeywordSaveSaveService;

    @Autowired
    private ProductService productService;


    /**
     * 搜索商品
     *
     * @param request 参数请求对象
     * @return
     * @throws IOException
     * @apiNote 根据用户提供的文本搜索商品
     * @Param sortField 排序字段 可选 "base_price", "sale_count"
     * @Param sortMethod 排序方式 可选值 asc desc
     */
//    @ExcludeFromResponseAdvice
    @GetMapping("searchProduct")
    public SearchResult<ProductDoc> searchProduct(@Valid ProductSearchRequest request) throws IOException {
        historyKeywordSaveSaveService.save(request.getText(), request.getMemberId());

        return productService.search(request);
    }

    /**
     * 查询产品通过商品分类id
     *
     * @param request
     * @return
     * @throws IOException
     */
    @GetMapping("searchProductBycategoryId")
    public SearchResult<ProductDoc> searchProductBycategoryId(@Valid ProductSearchByCategoryRequest request) throws IOException {

        return productService.searchProductBycategoryId(request);
    }

    /**
     * 推荐产品
     *
     * @param memberId （必填）用户id
     * @param from     （选填）从第几个开始
     * @param size     （选填）长度
     * @return List<ProductDoc>
     * @throws IOException
     * @apiNote 根据接收的参数查询出用户给用户推荐的产品
     */
//    @ExcludeFromResponseAdvice
    @GetMapping("recommend/products")
    public SearchResult<ProductDoc> recommendProducts(@RequestParam Integer memberId, @RequestParam(defaultValue = "0", required = false) Integer from, @RequestParam(defaultValue = "10", required = false) Integer size) throws IOException {


        SearchResult<ProductDoc> searchResult = productService.recommendProducts(memberId, from, size);

        // 如果没有推荐产品，则随机获取10个产品
        if (searchResult.getItems().size() == 0) {
            searchResult = productService.radomProducts(10);
            searchResult.setPageNum(from);
        }


        return searchResult;
    }

    /**
     * 搜索-提示语
     *
     * @param keyword 所输入的关键词
     * @return 关键词数组数据
     * @throws IOException
     * @apiNote 根据用户输入的关键词，返回提示语
     */
    @GetMapping("tips")
    public List<String> getTips(@RequestParam String keyword) throws IOException {
        List<String> Tips = productService.getTips(keyword);
        return Tips;
    }

    /**
     * 同类推荐
     *
     * @param id 商品id
     */
    @GetMapping("relevant/products")
    public List<ProductDoc> relevant(@RequestParam String id) throws IOException {
        List<ProductDoc> list = productService.getRelevant(id);
        return list;
    }


}
